package com.aponteitor.business.dao;

import java.util.List;

import javax.persistence.NoResultException;

import com.aponteitor.business.model.Legenda;

/**
 * @author wellsantos@wellsantos.com
 */
public class LegendaDao extends BaseDAO<Legenda, Long> {

    @Override
    public void create(Legenda entity) {
        entity.setNome(entity.getNome());
        super.create(entity);
    }

    @SuppressWarnings("unchecked")
    public List<Legenda> findByCategoria(Integer categoria) {
        String sql = "select a from Legenda a where a.idCategoria = :categoria";
        return getEntityManager()
              .createQuery(sql)
              .setParameter("categoria", categoria)
              .getResultList();
    }

    public Legenda findByNome(String nome) {
        try {
            String sql = "select a from Legenda a where a.nome = :nome";
            return (Legenda) getEntityManager()
                  .createQuery(sql)
                  .setParameter("nome", nome)
                  .getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }
}
